<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="关联招标采购申请" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invitationId">
              <SelectPurchasingRequisitionInput
                v-model="model.invitationId"
                :disabled="formDisabled"
                :teamId="teamId"
                @back="materialNameBack"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <select-project-input disabled v-model="model.projectId" placeholder="请选择项目名称" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="团队名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
              <select-team-input placeholder="请选择团队" disabled v-model="model.teamId" :tdlx="3" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="定标意见单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="opinionId">
              <SelectPurchaseCalibrationOpinionInput :disabled="formDisabled || !model.invitationId" :teamId="teamId" :invitationId="model.invitationId" isTeamDisabled v-model="model.opinionId" placeholder="请关联定标意见单"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import SelectPurchaseCalibrationOpinionInput from '@/views/erp/purchase/procurement/modules/SelectPurchaseCalibrationOpinionInput'
  import SelectPurchasingRequisitionInput from '@/views/scy/components/SelectPurchasingRequisitionInput'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectProjectInput from '@/views/scy/components/SelectProjectInput'
  export default {
    name: 'ErpPurchaseCalitionUnitForm',
    components: {
      SelectPurchasingRequisitionInput,
      SelectTeamInput,
      SelectProjectInput,
      SelectPurchaseCalibrationOpinionInput
    },
    props: {
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      teamId: {
        type: String,
        default: '',
        required: false
      },
      dataSource: {
        type: Array,
        default: () => []
      }
    },
    data () {
      return {
        flag: false,
        model: {
        },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        validatorRules: {
          invitationId: [
              { required: true, message: '请选择关联招标采购申请!', trigger: 'change' }
          ]
        },
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs['form'].clearValidate();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      materialNameBack(data) {
        const material = data[0]
        this.$set(this.model, 'projectId', material.projectId) // 项目名称
        this.$set(this.model, 'teamId', material.teamId) // 团队名称
        this.$set(this.model, 'projectBudget', material.projectBudget) // 预算价
        this.$set(this.model, 'provisionBroad', material.provisionBroad) // 采购类型
        this.$set(this.model, 'businessSegment', material.businessSegment) // 业务类型
        this.$set(this.model, 'opinionId', null) // 置空定标意见单
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            // 判断新增编辑
            // if (this.flag) {
            //   // 新增
            // }
            if (this.dataSource && this.dataSource.length) {
              // 新增第一条数据后，若关联招标采购申请单后带出项目名称，则可以新增第二条数据，项目名称必须为同一个
              if (this.flag) {
                if (this.dataSource[0].projectId) {
                  if (this.dataSource[0].projectId != this.model.projectId) {
                    this.$message.warning('请关联同一个项目的招标采购申请单')
                    return false
                  }
                }
              } else {
                if (this.model.key != 0) {
                  if (this.dataSource[0].projectId) {
                    if (this.dataSource[0].projectId != this.model.projectId) {
                      this.$message.warning('请关联同一个项目的招标采购申请单')
                      return false
                    }
                  }
                }
              }
            }
            if (this.dataSource && this.dataSource.length) {
              if (this.flag) {
                var isSome = this.dataSource.some(value => {
                  return value.invitationId == this.model.invitationId
                })
                if (isSome) {
                  this.$message.warning('请勿选择相同招标采购申请明细')
                  return false
                }
              }
            }
            that.$emit('ok', this.model, this.flag);
          }
        })
      }
    }
  }
</script>